use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::helper::build_regex;

/// 静态路由表一般是路由器和三层交换机有这一块的数据
#[derive(Debug, Serialize, Deserialize, Default, JsonSchema, Clone)]
pub struct IPRouteTable {
    /// 源IP地址
    pub source_ipaddress: String,
    /// 源IP子网掩码
    pub source_netmask: String,
    /// 下一跳IP地址
    pub destination_ipaddress: String,
    /// 存活时间
    pub age: String,
}

impl IPRouteTable {
    pub fn table_values(value: &str) -> Vec<Self> {
        let mut tables = vec![];
        let re = build_regex(
            r".3.6.1.2.1.4.24.4.1.8.(\d+\.\d+\.\d+\.\d+).(\d+\.\d+\.\d+\.\d+).0.(\d+\.\d+\.\d+\.\d+) = INTEGER: (\d+)",
        );
        for cap in re.captures_iter(value) {
            tables.push(IPRouteTable {
                source_ipaddress: cap[1].to_owned(),
                source_netmask: cap[2].to_owned(),
                destination_ipaddress: cap[3].to_owned(),
                age: cap[4].to_owned(),
            })
        }
        tables
    }
}
